<?php
if($_SESSION['login'] != true) {
	die('Brak dostepu');
}
//------------------------------
define(ACTION_SAVE,			"actionSave");
define(ACTION_CHANGE_PASS, 	"actionChangePass");
define(ACTION_SHUT_DOWN,	"actionShutDown");
//------------------------------
if(IsSet($_GET['action'])) {
	$action = $_GET['action'];

	if($action == ACTION_SAVE) {
		$configName 	= $_POST['configName'];
		$configValue	= $_POST['configValue'];
		actionSave($configName, $configValue);
	} else if($action == ACTION_CHANGE_PASS) {
		$password		= $_POST['password'];
		$passwordConfirm= $_POST['passwordConfirm'];
		$passwordChangeResult = actionChangePass($password, $passwordConfirm);
		if($passwordChangeResult) {
			?>
			<div style="color: green;">Hasło zmieniono</div><br />
			<?php
		} else {
			?>
			<div style="color: red;">Nie udało się zmienić hasła</div><br />
			<?php
		}
	} else if($action == ACTION_SHUT_DOWN) {
		if(actionShutDown()) {
			?>
			<div style="color: green;">Wysłano sygnał wyłączenia</div><br />
			<?php
		} else {
			?>
			<div style="color: red;">Aplikacja nie jest włączona</div><br />
			<?php
		}
	}
}
//------------------------------
if(IsSet($_GET['searcherId'])) {
	$searcherId = $_GET['searcherId'];
}
//------------------------------
if(!$searcherId) {
	$sql = "SELECT name, value, description, config_type FROM \"" . $configDbPrefix . "config\" WHERE config_type != 'PASS' AND config_type != 'WORK' ORDER BY config_type, name;";
} else {
	$sql = "SELECT name, value FROM \"" . $configDbPrefix . "searchers_config\" WHERE searcher_id=$searcherId ORDER BY name;";
}
$db->query($sql);
$rows = $db->fetchArray();

/*if($portalId) {
	$db->query("SELECT name FROM portal WHERE portal_id=$portalId;");
	$rowsName = $db->fetchArray();
	$portalName = $rowsName[0]['name'];

	$db->query("SELECT name, value FROM portal_config WHERE portal_id=$portalId ORDER BY name;");
	$rowsPortal = $db->fetchArray();
	if($rowsPortal) {
		foreach($rowsPortal as $r) {
			$rowPortal[$r['name']] = $r['value'];
		}
	}
}*/

insertMenu(0);
insertNewPassword(0);
?>
<table>
	<?php
	if($portalId) {
		?>
		<tr class="bgGray">
			<td></td>
			<td colspan="3">Konfiguracja zaawansowana dla portalu <b><?php echo $portalName; ?></b>:</td>
		</tr>
		<?php
	}
	?>
	<tr class="bgWhite">
		<!--<th></th>-->
		<th>Nazwa zmiennej</th>
		<?php if(!$searcherId) { ?>
			<th>Opis</th>
		<?php } ?>
		<th>Wartość</th>
	</tr>
	<?php
	$i = 0;
	foreach($rows as $row) {
		$i++;
		if($i % 2 == 0) {
			?>
			<tr class="bgWhite">
			<?php
		} else {
			?>
			<tr class="bgGray">
			<?php
		}
		
		?>
			<!-- kolejna liczba -->
			<!--<td width="3%"><?php echo $i . '.' ?></td>-->
			<!-- nazwa zmiennej -->
			<td>
				<?php 
					$name = $row['name'];
					$maxNameLen = 20;
					if(!$searcherId && strlen($name) > $maxNameLen) {
						echo substr($name, 0, $maxNameLen) . '<br>' . substr($name, $maxNameLen);
					} else {
						echo $name;
					}
				?>
			</td>
			<?php if(!$searcherId) { ?>
			<!-- opis -->
			<td width="32%" style="color: gray; text-align: left; padding-right: 15px;">
				<small><?php echo charsetConv($row['description']) ?></small>
			</td>
			<?php } ?>
			<!-- wartosc -->
			<td width="38%">
				<?php
				$link = $PHP_SELF . '?configType=' . ADVANCED_CONFIG . '&action=' . ACTION_SAVE;
				?>
				<form method="POST" action="<?php echo $link ?>" style="display: inline;">
					<?php
					if($row['config_type'] == 'COMB') {
						?>
						<select name="configValue">
							<?php
							if($row['name'] == 'SCREEN_IS_ACTIVE') {
								?>
								<option value="1" <?php if($row['value'] == 1) echo 'selected="selected"' ?>>TAK</option>
								<option value="0" <?php if($row['value'] == 0) echo 'selected="selected"' ?>>NIE</option>
								<?php
							} else if($row['name'] == 'USER_AGENT') {
								?>
								<option <?php if($row['value'] == 'RANDOM')  echo 'selected="selected"' ?>>RANDOM</option>
								<option <?php if($row['value'] == 'CRAWLER') echo 'selected="selected"' ?>>CRAWLER</option>
								<option <?php if($row['value'] == 'IE') 	 echo 'selected="selected"' ?>>IE</option>
								<option <?php if($row['value'] == 'FIREFOX') echo 'selected="selected"' ?>>FIREFOX</option>
								<option <?php if($row['value'] == 'OPERA')   echo 'selected="selected"' ?>>OPERA</option>
								<option <?php if($row['value'] == 'CHROME')  echo 'selected="selected"' ?>>CHROME</option>
								<?php
							}
							?>
						</select>
						<?php
					} else {
						?>
						<input type="text"   name="configValue" value="<?php if($portalId) { echo $rowPortal[$row['name']]; } else { echo $row['value']; } ?>" size="28">
						<?php
					}
					?>
					<input type="hidden" name="configName"  value="<?php echo $row['name'] ?>">
					<button type="submit"><img src="images/save.png" alt="Zapisz"></button>
				</form>
			</td>
		</tr>
		<?
	}
	
	?>
</table>
<br>
<?php if(!$searcherId) { ?>
	<div align="center">
		<hr />
		Konfiguracja zaawansowana dla poszczególnych wyszukiwarek:&nbsp;&nbsp;&nbsp;
		<?php
			$db->query("SELECT searcher_id, name, icon FROM \"" . $configDbPrefix . "searchers\" ORDER BY name;");
			$rows = $db->fetchArray();
			foreach($rows as $row) {
				$srId = $row['searcher_id'];
				$name = $row['name'];
				$icon = $row['icon'];
				?>
				<a href="<?php echo 'indexConfiguration.php?configType=advancedConfig' . $PHP_SELF . '&searcherId=' . $srId; ?>"><img src="./images/searchers/<?php echo $icon; ?>" title="<?php echo $name; ?>" /></a>&nbsp;&nbsp;&nbsp;
				<?php
			}
		?>
		<hr />
	</div>
	<br>
<?php
}
insertNewPassword(1);
insertMenu(1);

/*
*	FUNKCJE
*/

function insertMenu($idN) {
	global $searcherId;
	?>
	<script type="text/javascript">
		$(document).ready(
			function(){
				$("#newPasswordLink<?php echo $idN ?>").click(
					function(){
						$("#newPassword<?php echo $idN ?>").toggle("normal");
					}
				);
			}
		);
	</script>
	<a href="../"><img src="images/previous.png " alt="Wstecz">&nbsp;Strona główna</a>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<?php
	if($searcherId) {
		?>
		<a href="javascript: history.go(-1)"><img src="images/previous.png " alt="Powrót" title="Powrót">Powrót</a>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<?php
	}
	?>
	<a href="#" id="newPasswordLink<?php echo $idN ?>" ><img src="images/save.png" alt="Zmień hasło">&nbsp;Zmień hasło</a>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<a href="<?php echo $PHP_SELF . '?configType=advancedConfig&action=' . ACTION_SHUT_DOWN ?>" ><img src="images/action.gif" alt="Zmień hasło">&nbsp;Wyłącz aplikację</a>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<br>
	<br>
	<?php
}

/*function insertMenuPortal() {
	?>
	<a href="../Configuration/indexConfiguration.php?configType=monitorAndManage"><img src="images/previous.png " alt="Wstecz">&nbsp;Monitoring i ustawienia</a>
	<br>
	<br>
	<?php
}*/

function insertNewPassword($idN) {
	$link = $PHP_SELF . '?configType=' . ADVANCED_CONFIG . '&action=' . ACTION_CHANGE_PASS;
	?>
	<form method="POST" action="<?php echo $link; ?>" id="newPassword<?php echo $idN ?>" style="display:none;">
		Nowe hasło:&nbsp;<input type="password" name="password">&nbsp;
		Powtórz hasło:&nbsp;<input type="password" name="passwordConfirm">&nbsp;
		<button type="submit"><img src="images/save.png">&nbsp;Zapisz</button>
	</form>
	<?php
}

function actionSave($configName, $configValue) {
	global $db;
	global $configDbPrefix;
	$db->query("UPDATE \"" . $configDbPrefix . "config\" SET value='$configValue' WHERE name='$configName';");
}

/*function actionSavePortal($configName, $configValue, $portalId) {
	global $db;
	if($configValue == '') {
		$db->query("DELETE FROM portal_config WHERE portal_id=$portalId AND name='$configName'");
	} else {
		$db->query("SELECT count(*) as number FROM portal_config WHERE portal_id=$portalId AND name='$configName'");
		$array = $db->fetchArray();
		$number = $array[0]['number'];
		if($number == 0) {
			$db->query("INSERT INTO portal_config(portal_id, name, value) VALUES($portalId, '$configName', '$configValue')");
		} else {
			$db->query("UPDATE portal_config SET value='$configValue' WHERE name='$configName' AND portal_id=$portalId");
		}
	}
}*/

function actionChangePass($pass, $passConfirm) {
	if($pass != '' && $passConfirm != '' && $pass == $passConfirm) {
		$pass = md5($pass);
		global $db;
		global $configDbPrefix;
		$db->query("UPDATE \"" . $configDbPrefix . "config\" SET value='$pass' WHERE name='CONFIG_PANEL_PASSWORD' AND config_type='PASS';");
		return true;
	}
	return false;
}

function actionShutDown() {
	global $db;
	global $configDbPrefix;
	$db->query("SELECT count(*) as liczba FROM \"" . $configDbPrefix . "config\" WHERE name='CRAWLER_IS_WORKING' AND value='YES';");
	$rows = $db->fetchArray();
	$liczba = $rows[0]['liczba'];
	if($liczba) {
		$db->query("UPDATE \"" . $configDbPrefix . "config\" SET value='SHUTTING_DOWN' WHERE name='CRAWLER_IS_WORKING' AND value='YES';");
		return true;
	}
	return false;
}

?>